package com.sciener.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.sciener.entity.Car;
import com.sciener.entity.User;
import com.sciener.service.CarService;
import com.sciener.service.UserService;

public class carServlet extends HttpServlet {
	
	private static CarService carservice = new CarService();

	
	public carServlet() {
		super();
	}

	
	public void destroy() {
		super.destroy(); 
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		/*if(action.equals("getAllusers"))
		{
			List<User> users = userservice.getAllUsers();
			out.println(JSONArray.fromObject(users));
		}
		else if(action.equals("login"))
		{
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			boolean iscucess = userservice.rightUserAndPwd(username,password);
			out.print(iscucess);
		}
		else if(action.equals("getCarInfoByuserid"))
		{
			String userid = request.getParameter("userid");
			int id = Integer.parseInt(userid);
			List<Car> cars = carservice.getCarInfoByuserid(id);
	//		out.println(cars);
			out.println(JSONArray.fromObject(cars));
		}else */if(action.equals("verify"))
		{
			String userid = request.getParameter("userid");
			String content = request.getParameter("content");
			String type = request.getParameter("type");
			String result = carservice.requestVerify(Integer.valueOf(userid),content,type);
			out.print(result);
		}else if(action.equals("getcarinfobyuserid"))
		{
			String userid = request.getParameter("userid");
			Car c = carservice.getCarInfoByuserid(Integer.valueOf(userid));
			if(c!=null)
			{
				out.println(JSONArray.fromObject(c));
			}
			else
			{
				out.print("nocar");
			}
		}
		
		out.close();
	}

	
	public void init() throws ServletException {
		
	}

}
